$(function () {
  var detail_datagrid = $("#detail_datagrid");
  var detail_dialog = $("#detail_dialog");
  var detail_form = $("#detail_form");
  var insureInfoSn = $("#insureInfoSn");
  /*--------dialog2---*/
  var case_form = $("#case_form");
  var detail_title = $("#detail_title");
  var detail_dialog2 = $("#detail_dialog2");
  /*--------回选---*/
  var show_reporter = $("#show_reporter");
  var show_gender = $("#show_gender");
  var show_tel = $("#show_tel");
  var show_caseTime = $("#show_caseTime");
  var show_caseSpot = $("#show_caseSpot");
  var show_insureInfoSn = $("#show_insureInfoSn");
  var show_carNum = $("#show_carNum");
  var show_accidentResponsibility = $("#show_accidentResponsibility");
  var show_accidentDescription = $("#show_accidentDescription");
  var show_remarks = $("#show_remarks");
  /*$("#box").on("click", 'a.showCaseInfo', function () {
    console.log(this);
  })*/

  /*-------------*/
  // 状态数据
  var stateMap = {
    text: {
      '0': '<span class="text-danger">没有该保单</span>',
      '1': '<span class="text-success">已理赔</span>',
      '2': '<span class="text-danger">保单失效</span>',
      '3': '<span class="text-warning">待验证</span>',
      '4': '<span class="text-info">验证完,待填理赔</span>',
      '5': '<span class="text-primary">待审核</span>',
      '6': '<span class="text-danger">被拒审</span>',
      '7': '<span class="text-primary">审核完,待付款</span>',
      '8': '<span class="text-danger">废弃单</span>',
    },
    // 是否可编辑
    editable: {
      '0': true,
      '1': false,
      '2': true,
      '3': true,
      '4': false,
      '5': false,
      '6': false,
      '7': false,
      '8': false,
    }
  }

  detail_datagrid.datagrid({
    url: "/caseInfo/queryDetail.do",
    fitColumns: true,
    fit: true,
    toolbar: "#detail_toolbar",
    striped: true,
    rownumbers: true,
    singleSelect: true,
    pagination: true,
    columns: [
      [
        // {field: 'id'},
        {
          field: 'status', title: '当前状态', width: 80, formatter: function (value) {
            return value ? stateMap.text[value  ] : '';
          }
        },
        {field: 'tel', title: '报案电话', width: 80},
        {field: 'caseSpot', title: '报案地点', width: 80},
        {
          field: 'insureInfoSn', title: '保单编号', width: 80
        },
        {
          field: 'carNum', title: '车牌号', width: 80
        },
        {
          field: 'accidentDescription', title: '事故描述', width: 80
        },
        {field: 'accidentResponsibility', title: '事故责任', width: 80},
        {
          field: 'identifier', title: '验证员工', width: 80, formatter: function (val) {
            return val ? val.username : '';
          }
        },
        {field: 'remarks', title: '备注', width: 80},
        {
          field: 'xxx', title: '案件详情', width: 80, formatter: function () {
            return '<a class="showCaseMsg">查看详情</a>';
          }
        },
        {
          field: 'compensationInfo', title: '赔损明细', width: 80, formatter: function () {
            return '<a class="showCaseInfo">修改明细</a>';
          }
        },
      ]]
  });

  $("#box").on("click", "a.showCaseInfo", function () {
    //获取当前选中的行
    var row = detail_datagrid.datagrid('getSelected');
    if (row.id) {
      $.get('/caseInfo/selectCompensationInfoById.do', {compensationInfoId: row.id}, function (data) {
        // 回显数据
        detail_form.form('load', data);
        insureInfoSn.textbox('setValue', row.insureInfoSn)
      })
      //打开弹出框
      detail_dialog.dialog('open')
    }
  }).on("click", "a.showCaseMsg", function () {
    //获取当前选中的行
    var row = detail_datagrid.datagrid('getSelected');
    show_reporter.text(row.reporter)
    show_gender.text(row.gender ? "男": "女")
    show_tel.text(row.tel)
    show_caseTime.text(row.caseTime)
    show_caseSpot.text(row.caseSpot)
    show_insureInfoSn.text(row.insureInfoSn)
    show_carNum.text(row.carNum)
    show_accidentResponsibility.text(row.accidentResponsibility)
    show_accidentDescription.text(row.accidentDescription)
    show_remarks.text(row.remarks)
    openSetTitle("查看报案信息")
  })
  var css = {
    width: '100%',
    height: '100px'
  };
  function clearForm() {
    case_form.form('clear');
    detail_dialog2.modal('hide');
  }
  function openSetTitle(str) {
    detail_title.html(str);
    detail_dialog2.modal('show');
  }

  detail_dialog.dialog({
    title: '赔损明细',
    draggable: 'true',
    width: 300,
    height: 385,
    buttons: "#form_btns",
    closed: "true",
    onClose: function () {
      //清空表单数据
      detail_form.form('clear');
    }
  })

  //使用一个对象统一管理事件的方法
  var methodObj = {
    /*提审*/
    audit: function () {
      var row = detail_datagrid.datagrid("getSelected");
      if (!row) {
        $.messager.alert("温馨提示", '亲,请选择一条数据', 'warning')
        return
      }
      $.messager.confirm('确认', '你确认要提审吗？', function (r) {
        if (!r) {
          return
        }
        $.get('/caseInfo/needAudit.do', {id: row.id}, function (data) {
          if (data.success) {
            $.messager.alert("温馨提示", '提审成功', 'info', function () {
              detail_datagrid.datagrid("reload");
            });
          } else {
            $.messager.alert("温馨提示", '提审失败：' + data.msg, "error");
            detail_datagrid.datagrid("reload");
          }
        });

      })
    },
    crash: function () {
      var row = detail_datagrid.datagrid("getSelected");
      if (!row) {
        $.messager.alert("温馨提示", '亲,请选择一条数据', 'warning')
        return
      }
      $.messager.confirm('确认', '你确认要废弃吗？', function (r) {
        if (!r) {
          return
        }
        $.get('/caseInfo/crash.do', {id: row.id}, function (data) {
          if (data.success) {
            $.messager.alert("温馨提示", '废弃成功', 'info', function () {
              detail_datagrid.datagrid("reload");
            });
          } else {
            $.messager.alert("温馨提示", '废弃失败：' + data.msg, "error");
            detail_datagrid.datagrid("reload");
          }
        });

      })
    },
    /*查询*/
    query: function () {
      //获取查询条件的值
      var value = $("#keyword").textbox('getValue');

      detail_datagrid.datagrid('load', {
        keyword: value
      })
    },
    /*保存按钮*/
    save: function () {
      //提交表单
      detail_form.form('submit', {
        url: '/caseInfo/saveOrUpdateDetail.do',
        onSubmit: function (params) {
          var row = detail_datagrid.datagrid('getSelected')
          params['caseInfoId'] = row.id
          console.log(params);
        },
        success: function (data) {
          //转换为json对象
          data = $.parseJSON(data);
          if (data.success) {
            //提示保险操作结果
            $.messager.alert('温馨提示', '保存成功!', 'info', function () {
              //关闭弹出框
              methodObj.cancel();
              //让datagrid重新查询数据
              detail_datagrid.datagrid('reload');
            });
          } else {
            $.messager.alert('温馨提示', data.msg, 'error');
          }
        }
      });
    },
    /*刷新功能*/
    refresh: function () {
      detail_datagrid.datagrid('reload');
    },
    /*取消按钮*/
    cancel: function () {
      //关闭弹出框
      detail_dialog.dialog('close');
    }
  };

  //统一绑定事件
  $("a[data-cmd]").click(function () {
    var methodName = $(this).data("cmd");
    methodObj[methodName]();
  });
});